﻿/*
// CHANGE LOG
mod 11 jul 07 - ddb
ver 1.03
	** Fix layering/depth issue with FlashTable, ScrollPane was using parent_mc.getNextHighestDepth() instead of parameter depth
		added 1 new instance property:
			private var depth_num:Number;
		added code to set instance property 'depth_num' using parameter value:
			depth_num = table_obj.depth_num;
		added code to use depth_num when instantiating ScrollPane:
			timeline_mc.createClassObject(mx.containers.ScrollPane, "table_sp", depth_num);
		
		
mod 16 jun 07 - ddb
	ver 1.02
	changed:
		
	added:
		toString() method
		className_str:String
		version_str:String

*/
/*
// METHOD TABLE:
// FlashTable methods
//
function FlashTable(p_table_obj:Object) {
private function initTable():Void {
private function drawTable():Void {
private function getStyleByID(p_styleID_str:String):Object {
private function toString():Void{
*/
class com.utils.FlashTable {
	private var className_str:String;
	private var version_str:String;
	//
	private var timeline_mc:MovieClip;
	private var content_str:String;
	private var table_obj:Object;
	private var table_sp:mx.containers.ScrollPane;
	private var table_mc:MovieClip;
	private var nextRowDepth_num:Number;
	private var nextRowY_num:Number;
	private var styles_array:Array;
	private var tableWidth_num:Number;
	private var tableHeight_num:Number;
	private var scrollPaneGutter_num:Number;
	private var depth_num:Number;
	//
	// constructor
	function FlashTable(p_table_obj:Object) {
		className_str = "  FlashTable";
		version_str = "v1.03";
		trace(this);
		/**
			table_obj.xml = myXML.table;
			table_obj.target_mc = SCOPE_obj;
			table_obj.contentLinkage_str = "emptyTable_mc";
			table_obj.depth_num = tableDepth_num;
		**/
		table_obj = p_table_obj;
		initTable();
	}
	private function initTable():Void {
		//trace("initTable()");
		timeline_mc = table_obj.target_mc;
		content_str = table_obj.contentLinkage_str;
		styles_array = table_obj.xml.styles_array;
		depth_num = table_obj.depth_num;
		scrollPaneGutter_num = 4;
		drawTable();
	}
	private function drawTable():Void {
		//trace("drawTable()");
		timeline_mc.createClassObject(mx.containers.ScrollPane, "table_sp", depth_num);
		timeline_mc.table_sp.move(table_obj.xml.attributes.xPos, table_obj.xml.attributes.yPos);
		timeline_mc.table_sp.hScrollPolicy = "off";
		timeline_mc.table_sp.vScrollPolicy = "auto";
		timeline_mc.table_sp.contentPath = table_obj.contentLinkage_str;
		table_mc = timeline_mc.table_sp.content;
		nextRowDepth_num = 100;
		nextRowY_num = scrollPaneGutter_num;
		// create rows
		for (var r:Number = 0; r < table_obj.xml.rows_array.length; r++) {
			//trace("row"+r);
			var rowXML:Object = table_obj.xml.rows_array[r];
			var row:MovieClip = table_mc.createEmptyMovieClip("row"+r+"_mc",nextRowDepth_num);
			var rowHeight_num:Number = 0;
			nextRowDepth_num++;
			row._x = scrollPaneGutter_num;
			row._y = nextRowY_num;
			// get max cell height (row height)
			for (var c:Number = 0; c < rowXML.cells_array.length; c++) {
				//trace("  cell"+c);
				var cellXML:Object = rowXML.cells_array[c];
				var cellData_obj:Object = new Object();
				cellData_obj.root = row;
				cellData_obj.id = c;
				cellData_obj.text = cellXML.text;
				cellData_obj.cellWidth = Number(cellXML.attributes.width);
				cellData_obj.strokeWidth = table_obj.xml.attributes.borderWidth;
				var cell:com.utils.TableCell = new com.utils.TableCell();
				var cell_mc:MovieClip = cell.create(cellData_obj);
				if (cell_mc._height > rowHeight_num) rowHeight_num = cell_mc._height;
				// remove
				cell_mc.suicide();
				removeMovieClip(cell_mc);
				//trace("cell_mc:"+cell_mc);
			}
			// create cells
			var nextCellX_num:Number = 0;
			for (var c:Number = 0; c < rowXML.cells_array.length; c++) {
				//trace("  cell"+c);
				var cellXML:Object = rowXML.cells_array[c];
				var cellData_obj:Object = new Object();
				cellData_obj.root = row;
				cellData_obj.id = c;
				cellData_obj.text = cellXML.text;
				cellData_obj.cellWidth = Number(cellXML.attributes.width);
				cellData_obj.cellHeight = rowHeight_num;
				cellData_obj.fillColor = getStyleByID(cellXML.attributes.styleID).attributes.fillColor;
				cellData_obj.strokeColor = getStyleByID(cellXML.attributes.styleID).attributes.strokeColor;
				cellData_obj.strokeWidth = table_obj.xml.attributes.borderWidth;
				cellData_obj.coords = new Array(nextCellX_num,0);
				//trace("coords:"+cellData_obj.coords);
				var cell:com.utils.TableCell = new com.utils.TableCell();
				var cell_mc:MovieClip = cell.create(cellData_obj);
				nextCellX_num += (cell_mc._width-(Number(table_obj.xml.attributes.borderWidth)*2));
			}
			nextRowY_num += row._height-Number(table_obj.xml.attributes.borderWidth)*2;
			tableWidth_num = row._width+scrollPaneGutter_num;
		}
		// table (scrollPane) size
		// if table is shorter than specified table height, resize scrollPane/shrink to fit
		var tempTableHeight_num:Number = (nextRowY_num + (scrollPaneGutter_num*2)-2)+2;
			if (tempTableHeight_num <= Number(table_obj.xml.attributes.height)) {
				// dynamic table height is less than xml's table height attribute
				// use actual content's height
				tableHeight_num = tempTableHeight_num;
			} else {
				// dynamic table height is greater than xml's table height attribute
				// use xml height
				tableHeight_num = Number(table_obj.xml.attributes.height);
				// add width of vScrollBar
				tableWidth_num += 16;
			}
		// size scrollPane
		
		timeline_mc.table_sp.setSize(tableWidth_num, tableHeight_num);
	}
	private function getStyleByID(p_styleID_str:String):Object {
		//trace("getStyleByID()");
		var style_obj:Object;
		for (var i:Number = 0; i < styles_array.length; i++) {
			var styleID_str:String = styles_array[i].attributes.id;
			if (styleID_str == p_styleID_str) {
				style_obj = styles_array[i];
				break;
			}
		}
		return style_obj;
	}
	private function toString():String {
		return className_str + " ("+version_str+")";
	}
}